//
//  JoTableCell.swift
//  JoCafe
//
//  Created by ZHUXIETONG on 15/9/2.
//  Copyright © 2015年 ZHUXIETONG. All rights reserved.
//

import UIKit
import Eelay
//@objc public protocol JoTableCellDelegate
//{
//    @objc optional func touch(cell:JoTableCell,actionID:String,model:NSMutableDictionary)
//}
//
//@objc public protocol JoViewDelegate
//{
//    @objc optional func touch(view:UIView,actionID:String,model:NSMutableDictionary)
//}


@objc public protocol JoTableCellDelegate:NSObjectProtocol
{
    func touch(cell:Any,actionID:String,model:NSMutableDictionary)
}

public protocol JoViewDelegate:NSObjectProtocol
{
    func touch(view:UIView,actionID:String,model:NSMutableDictionary)
}





extension JoView {
    
    
//    open func fillPropertyViews() {
//
//        let pnames = self.getAllPropertys()
//
//        for name in pnames
//        {
//            if let v = self.getValueOfProperty(property: name) as? UIView
//            {
//                addSubview(v)
//
//            }
//        }
//    }
}

open class JoView:UIView,MoAtrributeProtocol {
    open func mo_set_controller(controller: UIViewController) {
        
    }
    
    open func mo_change_localizable(obj: Any?) {
        
    }

    
    public var jo_action:(String,NSMutableDictionary) ->Void  = {_,_  in}
    
    
    required public override init(frame: CGRect) {
        super.init(frame: frame)
        weak var wself = self
        __notice.add(task: (TP.Notice.localizableDidChange,{_ in
            wself?.mo_change_localizable(obj: nil)
        }))
        willLayoutRules()
        addLayoutRules()
        didLayoutRules()
    }
    required public init()
    {
        super.init(frame: CGRect.zero)
        weak var wself = self
        __notice.add(task: (TP.Notice.localizableDidChange,{_ in
            wself?.mo_change_localizable(obj: nil)
        }))
     
        willLayoutRules()
        addLayoutRules()
        didLayoutRules()
    }
    var didLayout = false
    
    open override func willMove(toWindow newWindow: UIWindow?) {
        
    }
//    open override func willMove(toSuperview newSuperview: UIView?) {
//        if !didLayout{
//            if let sv = superview{
//                self.mo_theme = sv.mo_theme
//            }
//            willLayoutRules()
//            addLayoutRules()
//            didLayoutRules()
//            didLayout = true
//        }
//    }
    
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
      
    }
    
    open func addLayoutRules()
    {
//        self.fillPropertyViews()
    }
    
    
    open func willLayoutRules()
    {
        //        self.fillPropertyViews()
    }
    
    open func didLayoutRules()
    {
        //        self.fillPropertyViews()
    }
    
    
    open func mo_model_reload() {
        
    }
    open func mo_detail_reload() {
        
    }
    
    public var needReload = true
    open override func layoutSubviews() {
        super.layoutSubviews()
        if needReload{
            didLoad()
            needReload = false
        }
    }
    
    open func didLoad(){
        
    }
    
}


public protocol JoCellLay:class {
    var contentView: UIView { get }
    var __indexPath:IndexPath{get set}
    func addLayoutRules()
}

extension JoTableCell:MoAtrributeProtocol
{
    
    
}


open class JoCheckedCell: JoTableCell {
    
    open func updateIsChecked() {
        if mo.model[FK.selected,""] == "YES"
        {
            contentView.backgroundColor = UIColor.white
        }
        else{
            contentView.backgroundColor = UIColor.table_bg_color
        }
    }
    
    open var isChecked:Bool{
        set{
            mo.model[FK.selected] = newValue ? FK.YES : FK.NO
            self.updateIsChecked()
        }
        get{
            return mo.model[FK.selected,""] == FK.YES
        }
    }
}

open class JoTableCell: UITableViewCell,JoCellLay {
    public var __indexPath: IndexPath = IndexPath(row: 0, section: 0)
    public var __line = UIView()
    public weak var delegate:JoTableCellDelegate?
    public weak var content_controller:UIViewController?
    public weak var tableView:UITableView?{
        get{
            return (delegate as? ListController)?.tableView
        }
    }
    
    public var needRefresh = true
    open func addOneView(view: UIView) {
        contentView.addSubview(view)
    }
    
    open func addStyles()
    {
        
    }
 
    
    
    
    open func active(ID actionID:String,object:NSMutableDictionary? = nil) {
        
        if let obj = object
        {
            self.delegate?.touch(cell: self, actionID: actionID, model: obj)

        }
        else
        {
            self.delegate?.touch(cell: self, actionID: actionID, model: mo.model)
        }
    }
    
        
    open func addLayoutRules()
    {
//        self.addPropertyViews()
        self.addStyles()
    }
    

    public override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        NotificationCenter.default.addObserver(self, selector: #selector(JoTableCell.needRefeshNotiy), name: NSNotification.Name(rawValue: UpdateCellRefreshStatus), object: nil)


        self.addSubview(__line)
        backgroundColor = ._cellBackground
        __notice.observer(TP.Notice.localizableDidChange) {[weak self] (t, ntype) in
            switch ntype.name.rawValue{
            case TP.Notice.localizableDidChange.name.rawValue:
                if let n = self as? MoAtrributeProtocol{
                    n.mo_change_localizable(obj: nil)
                }
            default:
                break
            }
        }

        addLayoutRules()
        didLayoutRules()
    }
    open func didLayoutRules(){
        
    }

    
    open class func RefreshAble()
    {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: UpdateCellRefreshStatus), object: nil)
    }
    
    
    @objc open func needRefeshNotiy()
    {
        self.needRefresh = true
    }
    
    
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.selectionStyle = .none
        NotificationCenter.default.addObserver(self, selector: #selector(needRefeshNotiy), name: NSNotification.Name(rawValue: UpdateCellRefreshStatus), object: nil)
        addLayoutRules()
    }
    
    
    
    
    override open func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override open func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    
    deinit
    {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: UpdateCellRefreshStatus), object: nil)
        
        
    }
    
    open func mo_set_controller(controller: UIViewController) {
        
    }
    
    
    open func mo_model_reload() {
//        contentView.fillData(model: self.mo.model)
    }
    
    open func mo_detail_reload() {
    
    }
    
    open func mo_model_willSet(new: NSMutableDictionary) {
        
    }
    open func mo_model_relase(current: NSMutableDictionary) {
        
    }
    
    
}
open class JoHeaderFooterView: UITableViewHeaderFooterView,MoAtrributeProtocol {
    open func mo_set_controller(controller: UIViewController) {
        
    }
    
    
    init() {
        super.init(reuseIdentifier: nil)
        addLayoutRules()
    }
    
    public override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
        addLayoutRules()
    }
    
    
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        addLayoutRules()
    }
    

    open func mo_model_reload(){
        
    }
    open func mo_detail_reload() {
    }
    
    
    open func addLayoutRules()
    {
        
    }
    
    
    
}






public protocol JoCollectionCellDelegate:NSObjectProtocol
{
    func touch(cell:Any,actionID:String,model:NSMutableDictionary)
}

@objc public protocol JoActionDelegate
{
    @objc optional func doAction(actionID:String,model:NSMutableDictionary)
}

//protocol UpdateRefreshStatus{
//    var needRefresh:Bool {get  set}
//    func addUpdateRefreshObserver()e
//    func addUpdateRefreshObserver()
//
//}
//


let UpdateCellRefreshStatus = "UpdateCellRefreshStatus"


open class JoCollectionCell: UICollectionViewCell,MoAtrributeProtocol,JoCellLay {
    open func mo_set_controller(controller: UIViewController) {
        
    }
    
    public var __indexPath: IndexPath = IndexPath(row: 0, section: 0)
    
    public var needRefresh = true
    
    public weak var delegate:JoCollectionCellDelegate?
    public weak var content_controller:UIViewController?{
        if let ctr = self.delegate as? UIViewController{
            
            return ctr
        }
        return nil
    }
    let __notice: EasyNotification = EasyNotification()
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        __notice.observer(TP.Notice.localizableDidChange) {[weak self] (t, ntype) in
            switch ntype.name.rawValue{

            case TP.Notice.localizableDidChange.name.rawValue:
                if let n = self{
                    n.mo_change_localizable(obj: nil)
                }
            default:
                break
            }
        }

        
        addLayoutRules()

        NotificationCenter.default.addObserver(self, selector: #selector(needRefeshNotiy), name: NSNotification.Name(rawValue: UpdateCellRefreshStatus), object: nil)
        
    }
    
    open override func _width() ->CGFloat
    {
        return UIScreen.main.bounds.size.width
    }
    
    open override func _height() ->CGFloat
    {
        return 0
    }

    
    open func active(ID actionID:String,object:NSMutableDictionary? = nil) {
        
        if let obj = object
        {
            self.delegate?.touch(cell: self, actionID: actionID, model:obj)
        }
        else
        {
            self.delegate?.touch(cell: self, actionID: actionID, model: mo.model)
        }
    }
    

    open func mo_model_willSet(new: MoAtrribute) {
    }
    
    
    @objc open func needRefeshNotiy()
    {
        self.needRefresh = true
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    open func addLayoutRules()
    {
//        self.addPropertyViews()

    }

    open func mo_model_reload() {
//        contentView.fillData(model: self.mo.model)

    }
    
    override open func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    
    deinit{
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: UpdateCellRefreshStatus), object: nil)
    }
    
    
}


extension JoView{
    private struct __AssociatedKeys {
        static var notify_key = "__easy_notify_key"
    }
    
    public var __notice: EasyNotification {
        get {
            if let obj = objc_getAssociatedObject(self, &__AssociatedKeys.notify_key) as? EasyNotification
            {
                return obj
            }
            let obj = EasyNotification()
            self.__notice = obj
            return obj
            
        }
        set {
            objc_setAssociatedObject(self, &__AssociatedKeys.notify_key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

extension JoTableCell{
    private struct __AssociatedKeys {
        static var notify_key = "__easy_notify_key"
    }
    
    public var __notice: EasyNotification {
        get {
            if let obj = objc_getAssociatedObject(self, &__AssociatedKeys.notify_key) as? EasyNotification
            {
                return obj
            }
            let obj = EasyNotification()
            self.__notice = obj
            return obj
            
        }
        set {
            objc_setAssociatedObject(self, &__AssociatedKeys.notify_key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
}


